package com.zxm.sunshineaiary.util;

import java.util.Map;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

public class PreferencesUtil {
	private Context context;
	private SharedPreferences preferences = null;
	private Editor editor = null;

	public PreferencesUtil(Context context) {
		this.context = context;
	}

	/**
	 * ��������Ĳ���(Ψһ��)
	 * 
	 * @param name
	 *            SharePreference�ļ���
	 * 
	 * @param key
	 *            keyֵ
	 * 
	 * @param value
	 *            valueֵ
	 */
	public void saveOnlyParameters(String name, String key, String value) {
		preferences = context.getSharedPreferences(name, Context.MODE_APPEND);
		editor = preferences.edit();
		editor.putString(key, value);
		// �ύ��ݵ������xml�ļ���
		editor.commit();
	}

	/**
	 * 
	 * @param name
	 * @param key
	 * @param position
	 */
	public void saveOnlyPosition(String name, String key, int position) {
		preferences = context.getSharedPreferences(name, Context.MODE_APPEND);
		editor = preferences.edit();
		editor.putInt(key, position);
		editor.commit();
	}

	/**
	 * ��ȡSharePreference�е��������
	 * 
	 * @param name
	 *            SharePreference�ļ���
	 * @return SharePreference�е��������
	 */
	public Map<String, ?> getParameterPreferences(String name) {
		preferences = context.getSharedPreferences(name, Context.MODE_PRIVATE);
		return preferences.getAll();
	}

	@SuppressWarnings("unchecked")
	public Map<String, Integer> getParameterPosition(String name) {
		Map<String, Integer> result = null;
		preferences = context.getSharedPreferences(name, Context.MODE_PRIVATE);
		result = (Map<String, Integer>) preferences.getAll();
		return result;
	}

	/**
	 * ��ȡ����Ĳ���(Ψһ��)
	 * 
	 * @param key
	 *            keyֵ
	 * @param name
	 *            SharePreference�ļ���
	 */
	public String getParameterSharePreference(String key, String name) {
		Map<String, ?> imageMap = getParameterPreferences(name);
		if (imageMap != null && !imageMap.isEmpty())
			return imageMap.get(key) + "";
		else
			return null;
	}

	/**
	 * 
	 * @param key
	 * @param name
	 * @return
	 */
	public Object getPosition(String key, String name) {
		Map<String, ?> imageMap = getParameterPreferences(name);
		if (imageMap != null && !imageMap.isEmpty())
			return imageMap.get(key);
		else
			return null;
	}

	public boolean getShare() {
		return false;
	}
}
